<?php

namespace App\Models;

use App\Traits\ModelEventTrait;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Services\UploadService;
/**
 * @property int            $file_id
 * @property string         $file_drive
 * @property int            $group_id
 * @property string         $file_path
 * @property string         $file_name
 * @property string         $file_type
 * @property string         $file_ext
 * @property int            $file_size
 * @property string         $acl
 * @property string         $file_thumb
 * @property string         $file_url
 */
class UploadFile extends Model
{
    use ModelEventTrait;
    use SoftDeletes;

    const KEY = 'file_id';
    const TABLE = 'sp_upload_file_group';

    protected $table = self::TABLE;
    protected $primaryKey = self::KEY;
    protected $connection = 'mysql';

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'update_time';
    const DELETED_AT = 'delete_time';

    protected $guarded = [];

    protected $hidden = [
        self::DELETED_AT,
    ];

    protected $casts = [
        'file_id' => 'integer',
        'group_id' => 'integer',
        'file_size' => 'integer',
        'create_time' => 'datetime:Y-m-d H:i:s',
        'update_time' => 'datetime:Y-m-d H:i:s',
    ];
    const FILE_TYPE_IMAGE = 'image';
    const FILE_TYPE_VIDEO = 'video';
    const FILE_TYPE_DOC = 'doc';
    const UPLOAD_DRIVE_LOCAL = 'local';
    const UPLOAD_DRIVE_ALIYUN_MIRROR = 'aliyun_mirror';

    const ACL_DEFAULT = 'default';
    const ACL_PUBLIC = 'public-read';

    const USER_TYPE_ADMIN = 'admin';


    public function getFileUrlAttribute($value): string
    {
        try {
            $upload_service = new UploadService();

            return $upload_service->getObjectUrl($value);
        } catch (\Throwable $exception) {
            return $value;
        }
    }

    public function getFileThumbAttribute($value): string
    {
        try {
            $upload_service = new UploadService();
            return $upload_service->getObjectThumb($this, $value);
        } catch (\Throwable $exception) {
            return $value;
        }
    }

    /**
     * 获取未被修改过的值
     *
     * @param $key
     * @param $default
     * @return mixed|string
     */
    public function getOriginalValue($key, $default = '')
    {
        return $this->attributes[$key] ?? $default;
    }
}
